// Copyright (c) 2010 SuccessFactors, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//     * Redistributions of source code must retain the above
//       copyright notice, this list of conditions and the following
//       disclaimer.
//
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials
//       provided with the distribution.
//
//     * Neither the name of the SuccessFactors, Inc. nor the names of
//       its contributors may be used to endorse or promote products
//       derived from this software without specific prior written
//       permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.

package org.owasp.jxt.container;

import java.net.URL;
import junit.extensions.TestSetup;
import junit.framework.Test;
import org.codehaus.cargo.container.ContainerType;
import org.codehaus.cargo.container.InstalledLocalContainer;
import org.codehaus.cargo.container.configuration.ConfigurationType;
import org.codehaus.cargo.container.configuration.LocalConfiguration;
import org.codehaus.cargo.container.deployable.WAR;
import org.codehaus.cargo.container.installer.Installer;
import org.codehaus.cargo.container.installer.ZipURLInstaller;
import org.codehaus.cargo.container.property.GeneralPropertySet;
import org.codehaus.cargo.container.property.ServletPropertySet;
import org.codehaus.cargo.generic.DefaultContainerFactory;
import org.codehaus.cargo.generic.configuration.DefaultConfigurationFactory;
import org.codehaus.cargo.util.log.SimpleLogger;

/**
 * CargoTestSetup
 *
 * @author Jeffrey Ichnowski
 * @version $Revision: 8 $
 */
public class CargoTestSetup extends TestSetup {

    public static final int PORT = 18080;
    private static final boolean DEBUG = false;
    private InstalledLocalContainer _container;
    private String _containerType;
    private String _zipUrl;

    public CargoTestSetup(Test test, String containerType, String zipUrl) {
        super(test);
        _containerType = containerType;
        _zipUrl = zipUrl;
    }

    @Override
    protected void setUp() throws Exception {

        Installer installer = new ZipURLInstaller(new URL(_zipUrl));
        // installer.setInstallDir(String)
        installer.install();

        if (DEBUG) {
            System.out.println("Installed to: "+installer.getHome());
        }

        LocalConfiguration configuration = (LocalConfiguration)
            new DefaultConfigurationFactory().createConfiguration(
                _containerType, ContainerType.INSTALLED, ConfigurationType.STANDALONE);

        configuration.setProperty(ServletPropertySet.PORT, ""+ PORT);
        if (DEBUG) {
            configuration.setProperty(GeneralPropertySet.LOGGING, "high");
        }

        _container = (InstalledLocalContainer) new DefaultContainerFactory().createContainer(
            _containerType, ContainerType.INSTALLED, configuration);
        _container.setHome(installer.getHome());
        if (DEBUG) {
            _container.setLogger(new SimpleLogger());
        }

        String warFile = System.getProperty("war-file");
        if (DEBUG) {
            System.out.println(warFile);
        }
        configuration.addDeployable(new WAR(warFile));
        if (DEBUG) {
            configuration.setLogger(new SimpleLogger());
            System.out.println("Starting container");
        }

        _container.start();
    }

    @Override
    protected void tearDown() throws Exception {
        if (DEBUG) {
            System.out.println("Stopping container");
        }
        _container.stop();
    }
} // CargoTestSetup
